﻿<%@ Page Title="" Language="C#" MasterPageFile="~/TsSite.Master" AutoEventWireup="true" CodeBehind="ServicePlanning.aspx.cs" Inherits="TireService.Web.View.ServiceView.ServicePlanning" %>

<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallbackPanel" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxPivotGrid" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxSplitter" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>

<%@ Register src="../../UserControls/ucFindAvailableServiceCarScheduleDetail.ascx" tagname="ucFindAvailableServiceCarScheduleDetail" tagprefix="uc4" %>

    

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <uc4:ucFindAvailableServiceCarScheduleDetail ID="ucFindAvailableServiceCarScheduleDetail" runat="server" />

    <script type="text/javascript">

        var TaskId = 0;
        var MultipleTaskId;

        // Function that runs when dropdown container selection changed
        function OnSelectedContainerChanged(dropdownlist) {
            ddlArea.PerformCallback(dropdownlist.GetValue());
            gvServiceDriveTasks.PerformCallback(dropdownlist.GetValue());
            pgWeekPlan.PerformCallback(dropdownlist.GetValue());
        }

        // Function that runs when dropdown area selection changed
        function OnSelectedAreaChanged(s, e) {

            gvServiceDriveTasks.PerformCallback(ddlContainer.GetValue());
            //ddlArea.PerformCallback(dropdownlist.GetValue());
            
            //gvServiceDriveTasks.PerformCallback(dropdownlist.GetValue());
            //pgWeekPlan.PerformCallback(dropdownlist.GetValue());
        }
        

        function SelectedServiceDriveTaskChanged(s, e) {
            // Get taskid
            gvServiceDriveTasks.GetRowValues(gvServiceDriveTasks.GetFocusedRowIndex(), 'TaskId', SetSelectedServiceDriveTaskId);
        }

        function SetSelectedServiceDriveTaskId(value) {
            if (value == null) {
                TaskId = 0;
            }
            else {
                TaskId = value;
            }
        }

        function LoadDayPlan(columnindex, rowindex) {
            // Update taskid
            gvServiceDriveTasks.GetRowValues(gvServiceDriveTasks.GetFocusedRowIndex(), 'TaskId', SetSelectedServiceDriveTaskId);


            // JAND: Test 5/3-2013
            gvServiceDriveTasks.GetSelectedFieldValues('TaskId', GetSelectedTasks);

            var input = columnindex + "|" + rowindex;

            callBackLoadDayPlan.PerformCallback(input);
        }

        // Perform callback that updates booked services as customer approved for selected bookings
        function GetSelectedTasks(selectedValues) {
            if (selectedValues.length == 0) return;

            MultipleTaskId = selectedValues;

            //var input = "CustomerApproved" + '|' + selectedValues.toString().replace(",", "|");
        }

        function OnCallBackLoadDayPlanCompleted(s, e) {
            if (e.result == '') return;

            // Runs client function in usercontrol (below function does only existing in usercontrol)
            //ShowDayPlanModalPopup(this, e.result, TaskId);

            // JAND: Test 5/3-2013
            ShowDayPlanModalPopup(this, e.result, TaskId, MultipleTaskId);
        }

    </script>

    <dx:ASPxCallback ID="callBackLoadDayPlan" ClientInstanceName="callBackLoadDayPlan" runat="server" OnCallback="callBackLoadDayPlan_Callback">
        <ClientSideEvents CallbackError="CallBackError" CallbackComplete="OnCallBackLoadDayPlanCompleted" />
    </dx:ASPxCallback>

    <!-- Javascript for finding available capacity -->
    <script language="javascript" type="text/javascript">

        // How to implement UserControl in page
        // Functions for finding available capacity
        function FindScheduleDetailModalPopup(element, key) {
            callbackFindScheduleDetailPanel.SetContentHtml("");
            pcFindScheduleDetail.ShowAtElement(element);
            callbackFindScheduleDetailPanel.PerformCallback(key);
            FindAvailableServiceCarScheduleDetailKeyValue = key;
        }

        function FindScheduleDetailRefreshParent() {
            //Perform action on parent control - ex. refresh gridview with PerformCallback
            gvServiceDriveTasks.PerformCallback(ddlContainer.GetValue());
            pgWeekPlan.PerformCallback(ddlContainer.GetValue());
        }

    </script>

    <table cellpadding="0" cellspacing="2" border="0">
        <tr>
            <td>
                <img id="imgServicePlanning" alt="" src="~/Images/Medium/stopwatch.png" runat="server" style="width: 32px; height: 32px" />
            </td>
            <td valign="middle">
                <h3><asp:Label ID="lblServicePlanning" runat="server" Text="Planlægning"></asp:Label></h3>
            </td>
        </tr>
    </table>

    <dx:ASPxSplitter ID="ASPxSplitter1" runat="server" Width="100%" Height="600px">
        <Styles>
            <Pane>
                <Paddings Padding="0px" />
            </Pane>
        </Styles>
        <Panes>
            <dx:SplitterPane MinSize="350px">
                <Panes>
                    <dx:SplitterPane Name="taskContainer" Size="60%" MinSize="100px">
                        <ContentCollection>
                            <dx:SplitterContentControl ID="SplitterContentControl3" runat="server">
                                <table>
                                    <tr>
                                        <td>
                                            <dx:ASPxLabel ID="lblSelectedContainer" runat="server" Text="Valgt lager">
                                            </dx:ASPxLabel>
                                        </td>
                                        <td>
                                            <dx:ASPxComboBox ID="ddlContainer" ClientInstanceName="ddlContainer" runat="server">
                                                <ClientSideEvents SelectedIndexChanged="function(s, e) { OnSelectedContainerChanged(s)  }" />
                                            </dx:ASPxComboBox>
                                        </td>
                                        <td>
                                            <dx:ASPxComboBox ID="ddlArea" ClientInstanceName="ddlArea" runat="server" OnCallback="ddlArea_Callback">
                                                <ClientSideEvents SelectedIndexChanged="function(s, e) { OnSelectedAreaChanged(s)  }" />
                                            </dx:ASPxComboBox>
                                        </td>
                                        <td>
                                            <dx:ASPxButton ID="btnRefreshServiceDriveTasks" ClientInstanceName="btnRefreshServiceDriveTasks" runat="server" Text="Opdater" AutoPostBack="False" UseSubmitBehavior="False">
                                                <ClientSideEvents Click="function(s, e) { OnSelectedContainerChanged(ddlContainer) }" />
                                            </dx:ASPxButton>
                                        </td>
                                        <td>
                                            <dx:ASPxButton ID="btnCreateServiceCarCapacity" ClientInstanceName="btnCreateServiceCarCapacity" runat="server" Text="Opret kapacitet" AutoPostBack="False" UseSubmitBehavior="False">
                                                <ClientSideEvents Click="function(s, e) { ShowCreateServiceCarCapacity() }" />
                                            </dx:ASPxButton>    
                                        </td>
                                    </tr>
                                </table>
                                <table width="100%">
                                    <tr>
                                        <td>
                                            <%--OnHtmlRowPrepared="gvCarNeedService_HtmlRowPrepared"  
                                                OnCustomCallback="gvCarNeedService_CustomCallback">--%>
                                            <dx:ASPxGridView ID="gvServiceDriveTasks" runat="server" Caption="Servicekørsel opgaver" 
                                                ClientInstanceName="gvServiceDriveTasks" Width="100%" KeyFieldName="TaskId" AutoGenerateColumns="False" EnableCallBacks="True"
                                                OnCustomCallback="gvServiceDriveTasks_CustomCallback">
                                                <SettingsLoadingPanel Text="Henter&amp;hellip;" />
                                                <Border BorderWidth="0px" />
                                                <Settings ShowVerticalScrollBar="true" ShowHeaderFilterButton="true" ShowGroupPanel="false" ShowFilterRow="true" />
                                                <SettingsPager AlwaysShowPager="False" PageSize="10" Position="Bottom">
                                                    <Summary 
                                                        AllPagesText="Sider: {0} - {1} ({2} servicekørsler)" 
                                                        Text="Side {0} af {1} ({2} servicekørsler)" />
                                                </SettingsPager>
                                                <SettingsBehavior ProcessFocusedRowChangedOnServer="false" AllowFocusedRow="true" ConfirmDelete="True" AllowGroup="false" AllowDragDrop="false" />
                                                <ClientSideEvents FocusedRowChanged="function(s, e) { SelectedServiceDriveTaskChanged() }" />
                                                <Columns>
                                                    <dx:GridViewCommandColumn ShowSelectCheckbox="True" VisibleIndex="0">
                                                        <HeaderTemplate>
                                                            <input type="checkbox" onclick="gvServiceDriveTasks.SelectAllRowsOnPage(this.checked);" style="vertical-align:middle;" title="Vælg/Fravælg alle linjer på siden"></input>
                                                        </HeaderTemplate>
                                                        <HeaderStyle Paddings-PaddingTop="1" Paddings-PaddingBottom="1" 
                                                            HorizontalAlign="Center">
                                                        <Paddings PaddingTop="1px" PaddingBottom="1px"></Paddings>
                                                        </HeaderStyle>
                                                    </dx:GridViewCommandColumn>
                                                    <dx:GridViewDataColumn FieldName="TaskId" Visible="false" VisibleIndex="3" Caption="TaskId">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="CarId" Visible="false" VisibleIndex="4" Caption="CarId">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="CustomerId" Visible="false" VisibleIndex="4" Caption="CustomerId">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="TireSetId" Visible="false" VisibleIndex="4" Caption="TireSetId">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="ContainerId" Visible="false" VisibleIndex="4" Caption="ContainerId">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="PrimaryLongitude" Visible="false" VisibleIndex="4" Caption="PrimaryLongitude">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="PrimaryLatitude" Visible="false" VisibleIndex="4" Caption="PrimaryLatitude">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="SecondaryLongitude" Visible="false" VisibleIndex="4" Caption="SecondaryLongitude">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="SecondaryLatitude" Visible="false" VisibleIndex="4" Caption="SecondaryLatitude">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataTextColumn FieldName="ContainerNumber" VisibleIndex="5" Visible="false" Caption="Lager">
                                                    </dx:GridViewDataTextColumn>
                                                    <dx:GridViewDataColumn FieldName="RegistrationNumber" VisibleIndex="1" Caption="Reg.nr.">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataHyperLinkColumn FieldName="CustomerId" VisibleIndex="2" Caption="Kundenr">
                                                        <PropertiesHyperLinkEdit NavigateUrlFormatString="~/View/CustomerView/EditCustomerData.aspx?CustomerId={0}" TextField="CustomerId" Target="_blank">
                                                        </PropertiesHyperLinkEdit>
                                                    </dx:GridViewDataHyperLinkColumn>
                                                    <dx:GridViewDataColumn FieldName="PrimaryAddress" VisibleIndex="3" Caption="Adresse">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="PrimaryZipCode" VisibleIndex="4" Caption="Postnr">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="PrimaryCity" VisibleIndex="5" Caption="By">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="PrimaryCountryId" VisibleIndex="6" Caption="PrimaryCountryId" Visible="false">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="SecondaryAddress" VisibleIndex="7" Caption="Sek. Adr" Visible="true">
                                                    </dx:GridViewDataColumn>          
                                                    <dx:GridViewDataColumn FieldName="SecondaryZipCode" VisibleIndex="8" Caption="Sek. Postnr" Visible="true">
                                                    </dx:GridViewDataColumn>          
                                                    <dx:GridViewDataColumn FieldName="SecondaryCity" VisibleIndex="9" Caption="Sek. By" Visible="true">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="Distance" VisibleIndex="10" Caption="Km pri." ToolTip="Afstand fra lager til kundens primære adresse">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn FieldName="DistanceSecondary" VisibleIndex="11" Caption="Km sek." ToolTip="Afstand fra lager til kundens sekundære adresse">
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn VisibleIndex="12">
                                                        <DataItemTemplate>
                                                            <dx:ASPxButton ID="btnFindCapacity" runat="server" ClientInstanceName="btnFindCapacity" Text="Find plads" AutoPostBack="false"
                                                                ClientSideEvents-Click='<%# "function(s, e) { FindScheduleDetailModalPopup(this," + Container.KeyValue + "); }" %>'>
                                                            </dx:ASPxButton>                                           
                                                        </DataItemTemplate>
                                                    </dx:GridViewDataColumn>
                                                    <dx:GridViewDataColumn VisibleIndex="13">
                                                        <DataItemTemplate>
                                                            <dx:ASPxLabel ID="lblTaskDetail" runat="server" Text='<%# GetTaskDetailCount(Convert.ToInt32(Container.KeyValue)) %>'>
                                                            </dx:ASPxLabel>
                                                        </DataItemTemplate>
                                                    </dx:GridViewDataColumn>
                                                </Columns>
                                                <Templates>
                                                    <DetailRow>
                                                        Opgavedetaljer
                                                        <br />
                                                        <dx:ASPxGridView ID="gvTaskDetail" ClientInstanceName="gvTaskDetail" runat="server" KeyFieldName="TaskDetailId"
                                                            Width="100%" OnBeforePerformDataSelect="gvTaskDetail_DataSelect">
                                                            <Columns>
                                                                <dx:GridViewDataColumn FieldName="TaskDetailId" Caption="Id" VisibleIndex="1" />
                                                                <dx:GridViewDataColumn FieldName="TaskDetailTypeId" Caption="Type" Visible="false" VisibleIndex="2" />
                                                                <dx:GridViewDataColumn FieldName="Description" Caption="Beskrivelse" VisibleIndex="2" />
                                                                <dx:GridViewDataColumn FieldName="EstimatedTime" Caption="Estimeret tid" VisibleIndex="2" />
                                                                <dx:GridViewDataColumn FieldName="Amount" Caption="Antal" VisibleIndex="3" />
                                                                <dx:GridViewDataColumn FieldName="NoteToEmployee" Caption="Note" VisibleIndex="4" />
                                                                <dx:GridViewDataColumn FieldName="Completed" Caption="Afsluttet" VisibleIndex="5" />
                                                                <dx:GridViewDataColumn FieldName="Cancelled" Caption="Annulleret" VisibleIndex="6" />
                                                            </Columns>
                                                        </dx:ASPxGridView>
                                                    </DetailRow>
                                                </Templates>
                                                <SettingsDetail ShowDetailRow="true" />                                            
                                            </dx:ASPxGridView>
                                        </td>
                                    </tr>
                                </table>
                            </dx:SplitterContentControl>
                        </ContentCollection>
                    </dx:SplitterPane>
                    <dx:SplitterPane Name="capacityContainer" MinSize="100px" ShowCollapseForwardButton="True" ScrollBars="Horizontal">
                        <ContentCollection>
                            <dx:SplitterContentControl ID="SplitterContentControl4" runat="server">
                                <table>
                                    <tr>
                                        <td>
                                            <dx:ASPxDateEdit ID="txtSelectedStartDate" ClientInstanceName="txtSelectedStartDate" runat="server">
                                                <ClientSideEvents DateChanged="function(s, e) { pgWeekPlan.PerformCallback()  }" />
                                            </dx:ASPxDateEdit>
                                        </td>
                                        <td>
                                            <dx:ASPxDateEdit ID="txtSelectedEndDate" ClientInstanceName="txtSelectedEndDate" runat="server">
                                                <ClientSideEvents DateChanged="function(s, e) { pgWeekPlan.PerformCallback()  }" />
                                            </dx:ASPxDateEdit>
                                        </td>
                                        <td>
                                            <dx:ASPxButton ID="btnRefreshWeekPlan" runat="server" Text="Opdater" AutoPostBack="False" UseSubmitBehavior="False">
                                                <ClientSideEvents Click="function(s, e) { pgWeekPlan.PerformCallback()  }" />
                                            </dx:ASPxButton>
                                        </td>
                                    </tr>
                                </table>
                                <table>
                                    <tr>
                                        <td>
                                            <input runat="server" id="ColumnIndex" type="hidden" enableviewstate="true" />
                                            <input runat="server" id="RowIndex" type="hidden" enableviewstate="true" />
                                            <dx:ASPxPivotGrid ID="pgWeekPlan" runat="server" 
                                                ClientInstanceName="pgWeekPlan" Width="100%" 
                                                EnableCallBacks="true" 
                                                OnCustomCallback="pgWeekPlan_CustomCallback">
                                                <Fields>
                                                    <dx:PivotGridField Area="RowArea" AreaIndex="0" FieldName="ServiceCarId" ID="fieldServiceCarId" Caption="Servicevogn" />
                                                    <dx:PivotGridField Area="ColumnArea" AreaIndex="1" FieldName="ScheduleDate" 
                                                        ID="fieldScheduleDate" Caption="Dag" GroupInterval="DateDayOfWeek" 
                                                        UnboundFieldName="fieldScheduleDate" />
                                                    <dx:PivotGridField Area="ColumnArea" AreaIndex="0" FieldName="ScheduleDate" 
                                                        ID="PivotGridField1" Caption="Dato" GroupInterval="Date" 
                                                        UnboundFieldName="PivotGridField1" />
                                                    <dx:PivotGridField Area="ColumnArea" AreaIndex="2" 
                                                        FieldName="ScheduleDetailTypeDescription" ID="PivotGridField2" />
                                                    <dx:PivotGridField Area="DataArea" AreaIndex="0" SummaryType="Count" FieldName="ServiceCarScheduleDetailId" ID="fieldServiceCarScheduleDetailId" Caption="Kapacitet" TotalsVisibility="None" />
                                                </Fields>
                                                <OptionsView ShowDataHeaders="False" ShowRowHeaders="False" ShowFilterHeaders="False" ShowColumnHeaders="False" />
                                            </dx:ASPxPivotGrid>
                                        </td>
                                    </tr>
                                </table>

                            </dx:SplitterContentControl>
                        </ContentCollection>
                    </dx:SplitterPane>
                </Panes>
            </dx:SplitterPane>

        </Panes>
    </dx:ASPxSplitter>

</asp:Content>
